
//
//  FavoritesGroupDao.swift
//  Tinsecret_TW
//
//  Created by wanghui on 2018/12/6.
//  Copyright © 2018年 tst. All rights reserved.
//

import Foundation
import WCDBSwift

class FavoritesGroupDao {
    static func getAllEmotions() throws -> [Emotion]{
        var returnData = [Emotion]()
        let db = SQLiteHelper.getDb()
        let emotionGroups:[Emotion] = try db.getObjects(on: Emotion.CodingKeys.all, fromTable: Emotion.table)
        returnData = emotionGroups
        return returnData
    }
    
    static func getAllEmotionGroups() throws -> [FavoritesGroup]{
        var returnData = [FavoritesGroup]()
        let db = SQLiteHelper.getDb()
        let emotionGroups:[FavoritesGroup] = try db.getObjects(on: FavoritesGroup.CodingKeys.all, fromTable: FavoritesGroup.table)
        returnData = emotionGroups
        return returnData
    }
    
    static func getFavEmotionGroup(id:Int64) throws -> FavoritesGroup? {
        var returnData:FavoritesGroup?
        let db = SQLiteHelper.getDb()
        let emotionGroup:FavoritesGroup? = try db.getObject(on: FavoritesGroup.CodingKeys.all, fromTable: FavoritesGroup.table, where: FavoritesGroup.CodingKeys.id == id)
        returnData = emotionGroup
        return returnData
    }
    
    static func save(emotionGroup:FavoritesGroup...) throws {
        try save(emotionGroup: emotionGroup)
    }

    static func save(emotionGroup:[FavoritesGroup]) throws {
        let db = SQLiteHelper.getDb()
        try db.insert(emotionGroup, intoTable: FavoritesGroup.table)
    }
    
    static func delete(id:Int64) throws {
        let db = SQLiteHelper.getDb()
        try db.delete(fromTable: FavoritesGroup.table, where: FavoritesGroup.CodingKeys.id == id)
    }
}
